<?php
/**
 * SearchPortlet - Display part.
 *
 * @author Domingo Añez <domingo.aab@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2007,2008 Domingo Añez
 * @license http://www.pradoportal.dk
 * @version $Id: SearchPortlet.php 271 2008-11-07 07:56:57Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Portlets.Portlet');

/**
 * SearchPortlet
 */
class SearchPortlet extends Portlet
{
	const PORTLET_CONFIG_PREFIX	= "searchportlet_";
	private $_portletconfig	= null;

	public function getPortletConfig()
	{
		return $this->_portletconfig;
	}

	public function setPortletConfig($value)
	{
		$this->_portletconfig = $value;
	}

	public function onInit($param)
	{
		parent::onInit($param);
		$name	= self::PORTLET_CONFIG_PREFIX . $this->Parameters['id'];
		$crow	= $this->DataAccess->createCommand("select * from tblconfig where name='$name'")->queryRow();

		if(!$crow)
		{
			throw new TConfigurationException(Prado::localize('The {0} is not configured correctly',get_class($this)));
		}

		$this->_portletconfig		= unserialize($crow['value']);
		$this->_portletconfig['id']	= $crow['id'];
	}

	public function searchButtonClicked($sender,$param)
	{
		if($this->Page->IsValid)
		{
			// Find out output control
			$opControl	=  $this->Page->Master->findControl($this->PortletConfig['outputcontrol']);
			if($opControl && is_object($opControl))
			{
				// Create a TPanel for our result
				$tPanel						= new TPanel();
				$tPanel->CssClass			= "searchportlet"; // A css class with the same name as the portlet is 'required'
				$tPanel->Enableviewstate	= false; // Disable viewstate otherwise a second search will not work.

				// perform the search....
				$results = $this->PerformSearch($this->Search->Text);
				if(is_array($results) && count($results) != 0)
				{
					// Add a literal control and display information about the number of results, search criteria etc.
					$infoCtrl			= new TLiteral();
					$loctxt				= Prado::localize("Your search for '{0}' returned {1} result(s)", array($this->Search->Text,count($results)));
					$infoCtrl->Text		= "<br />$loctxt<br />";
					$tPanel->Controls[]	= $infoCtrl;

					foreach($results as $result)
					{
						// For each result we would a link to the page
						// For simplicity Hyperlinks are used
						$ctrlHypelink				= new THyperLink();
						$ctrlHypelink->NavigateUrl	= $this->Service->constructUrl($result['pagename']);
						$ctrlHypelink->Text			= $this->allhtmlentities($result['pagename']);

						// add to the panel
						$tPanel->Controls[] = $ctrlHypelink;
						$nlCtrl				= new TLiteral();
						$nlCtrl->Text		= "<br />";
						$tPanel->Controls[]	= $nlCtrl;
					}
				}
				else
				{
					// Add a control to tell the user that the search did not return any result
					$infoCtrl			= new TLiteral();
					$infoCtrl->Text		= "<br />" . Prado::localize("Sorry your search for '{0}' did not return any results", array($this->Search->Text)) . "<br />";
					$tPanel->Controls[]	= $infoCtrl;
				}

				// Add the result to the 'output' control
				$opControl->Controls[] = $tPanel;
			}
			else
			{
				$this->Page->reportError("output control '{$this->PortletConfig['outputcontrol']}' not found");
			}
		}
		$this->Search->Text = "";
	}

	public function PerformSearch($searchtext)
	{
		$searchQueries	= array();
		$seachresults	= array();
		$allPortlets	= $this->DataAccess->createCommand("select * from tblportlets where active=1")->query();

		if($allPortlets && count($allPortlets) > 0)
		{
			foreach($allPortlets as $portlet)
			{
				$portletns = "Application.Portlets.{$portlet['name']}.{$portlet['name']}";
				$portletfile = Prado::getPathOfNamespace($portletns,'.php');

				if(file_exists($portletfile))
				{
					Prado::using($portletns);
					if(class_exists($portlet['name']))
					{
						$portletObj		= new $portlet['name'];
						$psearchquery	= $portletObj->SearchQuery($searchtext);
						if($psearchquery !== null)
						{
							$searchQueries[] = $psearchquery;
						}
					}
				}
			}

			if(count($searchQueries) > 0)
			{
				$am			= $this->Application->getModule('auth');

				foreach($searchQueries as $sq)
				{
					$results = $this->DataAccess->createCommand($sq)->query();
					if($results->RowCount)
					{
						foreach($results as $result)
						{
							if($am->CanView($result['pagename']))
							{
								$seachresults[] = $result;
							}
						}
					}
				}
			}
		}

		return $seachresults;
	}
	public function allhtmlentities($string,$decode_first=true)
	{
		 // this is to ensure that any entities already coded are not "messed up"
		if($decode_first)
			$string = html_entity_decode($string);
		// "encode"
		return preg_replace('/([^\x09\x0A\x0D\x20-\x7F]|[\x21-\x2F]|[\x3A-\x40]|[\x5B-\x60])/e', '"&#".ord("$0").";"', $string);
	}
}
?>